package org.emet.web.ro;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.emet.dao.ash.AshItemDao;
import org.emet.entity.ash.AshHall;
import org.emet.entity.ash.AshItem;
import org.emet.entity.ash.Frame;
import org.emet.entity.ash.StorageInfo;
import org.emet.jee.BusinessException;
import org.emet.jee.core.dao.Page;
import org.emet.service.AshStorageService;
import org.emet.service.dto.ash.AshItemDTO;
import org.emet.service.dto.ash.AshItemSearchCriteria;
import org.emet.service.dto.ash.FrameDTO;
import org.emet.service.dto.ash.HallDTO;
import org.emet.service.dto.ash.StorageInfoDTO;
import org.emet.service.dto.ash.StorageInfoSearchCriteria;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Remote Object.
 * 
 * @author alex
 * 
 */

@Service
@RemotingDestination
@Transactional
public class AshManageRO {

	@Autowired
	private AshStorageService ashStorageService;
	@Autowired
	private AshItemDao ashItemDao;

	public void createFrame(Long hallID, FrameDTO f) {
		Frame entity = new Frame();
		entity.setAshHall(ashStorageService.getHall(hallID));
		entity.setCode(entity.getAshHall().getCode() + "." + f.getCode());
		entity.setMaxCol(f.getMaxCol());
		entity.setMaxRow(f.getMaxRow());
		entity.setName(f.getName());

		ashStorageService.saveFrame(entity);
	}

	public StorageInfoDTO getStorageInfoByAshItem(Long ashItemId) {
		StorageInfo storageInfo = ashStorageService
				.getStorageInfoByAshItem(ashItemId);
		StorageInfoDTO entryDTO = new StorageInfoDTO();
		copyProperties(storageInfo, entryDTO);
		return entryDTO;
	}

	public void updateFrame(FrameDTO f) {
		Frame entity = ashStorageService.getFrame(f.getId());
		entity.setId(f.getId());
		entity.setCode(entity.getAshHall().getCode() + "." + f.getCode());
		entity.setMaxCol(f.getMaxCol());
		entity.setMaxRow(f.getMaxRow());
		entity.setName(f.getName());

		ashStorageService.saveFrame(entity);
	}

	public void deleteSFrame(Long id) {
		ashStorageService.deleteFrame(id);
	}

	public HallDTO getHall(Long id) {
		AshHall dto = ashStorageService.getHall(id);
		HallDTO hall = new HallDTO();
		hall.setCode(dto.getCode());
		hall.setId(dto.getId());
		hall.setName(dto.getName());
		return hall;
	}

	public void createHall(HallDTO dto) {
		AshHall hall = new AshHall();
		hall.setCode(dto.getCode());
		hall.setName(dto.getName());

		ashStorageService.saveHall(hall);
	}

	public void updateHall(HallDTO dto) {
		AshHall hall = ashStorageService.getHall(dto.getId());
		hall.setCode(dto.getCode());
		hall.setName(dto.getName());

		ashStorageService.saveHall(hall);
	}

	public void deleteHall(Long id) {
		ashStorageService.deleteHall(id);
	}

	public Page<StorageInfoDTO> searchStorageInfo(Page<StorageInfo> page,
			StorageInfoSearchCriteria c) {
		try {
			ashStorageService.searchStorageInfo(page, c);

			Page<StorageInfoDTO> resultPage = new Page<StorageInfoDTO>();
			resultPage.setResult(new ArrayList<StorageInfoDTO>());
			BeanUtils.copyProperties(page, resultPage, new String[] { "result",
					"order" });

			for (StorageInfo entry : page.getResult()) {
				StorageInfoDTO entryDTO = new StorageInfoDTO();
				/*
				 * entryDTO.setFlowNo(entry.getFlowNo());
				 * entryDTO.setDeceasedName(entry.getDeceasedName());
				 * entryDTO.setDeceasedGender(entry.getDeceasedGender());
				 * entryDTO.setAshItemId(entry.getAshItemId());
				 * entryDTO.setAshItemCode(entry.getAshItemCode());
				 * entryDTO.setAshItemPosition(entry.getAshItemPosition());
				 * entryDTO.setTakeAway(entry.isTakeAway());
				 */

				copyProperties(entry, entryDTO);
				resultPage.getResult().add(entryDTO);
			}
			return resultPage;
		} catch (Exception e) {
			throw new BusinessException(e);
		}
	}

	public void deleteStorageInfo(Long id) {
		ashStorageService.deleteStorage(id);
	}

	public void createStorageInfo(StorageInfoDTO dto) {
		StorageInfo entity = new StorageInfo();
		copyProperties(dto, entity);
		ashStorageService.saveStorageInfo(entity);
	}

	public void updateStorageInfo(StorageInfoDTO dto) {
		StorageInfo entity = ashStorageService.getStorageInfo(dto.getId());
		if (entity.getAshItemId() != dto.getAshItemId()) {
			//位置被交换
			AshItem ashItem = ashItemDao.get(entity.getAshItemId());
			if (ashItem != null) {
				ashItem.setStatus(0);
				ashItem.setdName(null);
			}
		}
		copyProperties(dto, entity);
		ashStorageService.saveStorageInfo(entity);
	}

	public StorageInfoDTO get(Long id) {
		StorageInfo storageInfo = ashStorageService.getStorageInfo(id);
		StorageInfoDTO entryDTO = new StorageInfoDTO();

		BeanUtils.copyProperties(storageInfo, entryDTO);

		return entryDTO;
	}

	public List<HallDTO> getAllHall() {
		List<AshHall> allHalls = ashStorageService.getAllHalls();
		List<HallDTO> displayHalls = new ArrayList<HallDTO>();
		copyProperties(allHalls, displayHalls);
		return displayHalls;
	}

	public List<FrameDTO> getFrames(Long hallId) {
		List<FrameDTO> result = new ArrayList<FrameDTO>();
		List<Frame> frames = ashStorageService.getFrames(hallId);
		for (Frame frame : frames) {
			FrameDTO f = new FrameDTO();
			f.setId(frame.getId());
			f.setName(frame.getName());
			f.setCode(frame.getCode());
			f.setMaxCol(frame.getMaxCol());
			f.setMaxRow(frame.getMaxRow());
			result.add(f);
		}

		return result;
	}

	public List<AshItemDTO> searchAshItems(AshItemSearchCriteria criteria) {
		List<AshItemDTO> result = new ArrayList<AshItemDTO>();
		List<AshItem> r = ashStorageService.searchAshItems(criteria);

		for (AshItem ashItem : r) {
			AshItemDTO i = new AshItemDTO();
			i.setCode(ashItem.getCode());
			i.setPosition(createPosition(ashItem));
			i.setPrice(ashItem.getPrice());
			i.setName(ashItem.getName());
			i.setId(ashItem.getId());
			i.setdName(ashItem.getdName());
			i.setStatus(ashItem.getStatus());
			result.add(i);
		}

		return result;
	}

	public List<AshItemDTO> getAshItemsByRow(Long frameId, int rowNo) {
		List<AshItemDTO> result = new ArrayList<AshItemDTO>();
		List<AshItem> ashItems = ashStorageService
				.getAshItemRow(frameId, rowNo);
		for (AshItem ashItem : ashItems) {
			AshItemDTO f = new AshItemDTO();
			f.setCode(ashItem.getCode());
			f.setPrice(ashItem.getPrice());
			f.setFrame(null);
			f.setId(ashItem.getId());
			f.setNum(ashItem.getNum());
			f.setCol(ashItem.getCol());
			f.setRow(ashItem.getRow());
			f.setStatus(ashItem.getStatus());
			f.setdName(ashItem.getdName());
			f.setName(ashItem.getName());
			f.setPosition(createPosition(ashItem));

			result.add(f);
		}

		return result;
	}

	private String createPosition(AshItem ashItem) {
		return ashItem.getFrame().getAshHall().getName() + "-"
				+ ashItem.getFrame().getName() + "-" + (ashItem.getRow() + 1)
				+ "排 " + (ashItem.getCol() + 1) + "号";
	}

	public void createAshItem(Long frameId, AshItemDTO ai) {
		Frame frame = ashStorageService.getFrame(frameId);
		AshItem ashItem = new AshItem();
		ashItem.setFrame(frame);
		ashItem.setCode(ai.getCode());
		ashItem.setName(ai.getName());
		ashItem.setCol(ai.getCol());
		ashItem.setRow(ai.getRow());
		ashItem.setPrice(ai.getPrice());

		ashStorageService.saveAshItem(ashItem);
	}

	public void updateAshItem(AshItemDTO ai) {
		AshItem ashItem = ashStorageService.getAshItem(ai.getId());
		ashItem.setCode(ai.getCode());
		ashItem.setName(ai.getName());
		ashItem.setPrice(ai.getPrice());
		ashItem.setCol(ai.getCol());
		ashItem.setRow(ai.getRow());

		ashStorageService.saveAshItem(ashItem);
	}

	public void deleteAshItem(Long id) {
		ashStorageService.deleteAshItem(id);
	}

	public List<AshItemDTO> getAshItems(Long frameId) {
		List<AshItemDTO> result = new ArrayList<AshItemDTO>();
		List<AshItem> ashItems = ashStorageService.getAshItems(frameId);
		for (AshItem ashItem : ashItems) {
			AshItemDTO f = new AshItemDTO();
			f.setCode(ashItem.getCode());
			f.setPrice(ashItem.getPrice());
			f.setFrame(null);
			f.setId(ashItem.getId());
			f.setNum(ashItem.getNum());
			f.setCol(ashItem.getCol());
			f.setRow(ashItem.getRow());
			f.setStatus(ashItem.getStatus());
			f.setName(ashItem.getName());
			f.setdName(ashItem.getdName());
			f.setPosition(createPosition(ashItem));

			result.add(f);
		}

		return result;
	}

	public List<AshItemDTO> getAllAshItemsInHall(Long hallId) {
		List<AshItemDTO> result = new ArrayList<AshItemDTO>();
		List<AshItem> ashItems = ashStorageService.getAshItemsByHall(hallId);
		for (AshItem ashItem : ashItems) {
			AshItemDTO f = new AshItemDTO();
			f.setCode(ashItem.getCode());

			result.add(f);
		}

		return result;
	}

	public void takeAway2(String person, String remark, Date date, Long id) {
		ashStorageService.takeAway(person, remark, date, id);
	}

	private void copyProperties(List<AshHall> allHalls, List<HallDTO> displayHalls) {
		Iterator<AshHall> it = allHalls.iterator();
		while (it.hasNext()) {
			AshHall hall = it.next();

			HallDTO hdto = new HallDTO();
			hdto.setId(hall.getId());
			hdto.setName(hall.getName());
			hdto.setCode(hall.getCode());

			List<Frame> frames = hall.getFrames();
			List<FrameDTO> frameList = new ArrayList<FrameDTO>();

			for (Frame frame : frames) {
				FrameDTO fdto = new FrameDTO();
				fdto.setCode(frame.getCode());
				fdto.setId(frame.getId());
				fdto.setName(frame.getName());
				fdto.setMaxCol(frame.getMaxCol());
				fdto.setMaxRow(frame.getMaxRow());
				fdto.setHall(hdto);
				frameList.add(fdto);
			}

			hdto.setFrames(frameList);
			displayHalls.add(hdto);
		}
	}

	private void copyProperties(StorageInfoDTO dto, StorageInfo entity) {
		entity.setAddress(dto.getAddress());
		entity.setAge(dto.getAge());
		entity.setAshItemCode(dto.getAshItemCode());
		entity.setAshItemId(dto.getAshItemId());
		entity.setAshItemPosition(dto.getAshItemPosition());
		entity.setAshItemPrice(dto.getAshItemPrice());
		entity.setAudit(dto.isAudit());
		entity.setAuditUser(dto.getAuditUser());
		entity.setAuditUserSN(dto.getAuditUserSN());
		entity.setBirthDay(dto.getBirthDay());
		entity.setCertificateDay(dto.getCertificateDay());
		entity.setDate(dto.getDate());
		entity.setDeceasedGender(dto.getDeceasedGender());
		entity.setDeceasedName(dto.getDeceasedName());
		entity.setDieCause(dto.getDieCause());
		entity.setDieDay(dto.getDieDay());
		entity.setEndDay(dto.getEndDay());
		entity.setFlowNo(dto.getFlowNo());
		entity.setIdCard(dto.getIdCard());
		entity.setNationality(dto.getNationality());
		entity.setNativePlace(dto.getNativePlace());
		entity.setPhone(dto.getPhone());
		entity.setRace(dto.getRace());
		entity.setRelationShip(dto.getRelationShip());
		entity.setRelativeGender(dto.getRelativeGender());
		entity.setRelativeName(dto.getRelativeName());
		entity.setRemark(dto.getRemark());
		entity.setStartDay(dto.getStartDay());
		entity.setTakeAway(dto.isTakeAway());
		entity.setTakeWay(dto.getTakeWay());
		entity.setDeceasedPYName(dto.getDeceasedPYName());
		entity.setRelativePYName(dto.getRelativePYName());
	}

	private void copyProperties(StorageInfo dto, StorageInfoDTO entity) {
		entity.setId(dto.getId());
		entity.setAddress(dto.getAddress());
		entity.setAge(dto.getAge());
		entity.setAshItemCode(dto.getAshItemCode());
		entity.setAshItemId(dto.getAshItemId());
		entity.setAshItemPosition(dto.getAshItemPosition());
		entity.setAshItemPrice(dto.getAshItemPrice());
		entity.setAudit(dto.isAudit());
		entity.setAuditUser(dto.getAuditUser());
		entity.setAuditUserSN(dto.getAuditUserSN());
		entity.setBirthDay(dto.getBirthDay());
		entity.setCertificateDay(dto.getCertificateDay());
		entity.setDate(dto.getDate());
		entity.setDeceasedGender(dto.getDeceasedGender());
		entity.setDeceasedName(dto.getDeceasedName());
		entity.setDieCause(dto.getDieCause());
		entity.setDieDay(dto.getDieDay());
		entity.setEndDay(dto.getEndDay());
		entity.setFlowNo(dto.getFlowNo());
		entity.setIdCard(dto.getIdCard());
		entity.setNationality(dto.getNationality());
		entity.setNativePlace(dto.getNativePlace());
		entity.setPhone(dto.getPhone());
		entity.setRace(dto.getRace());
		entity.setRelationShip(dto.getRelationShip());
		entity.setRelativeGender(dto.getRelativeGender());
		entity.setRelativeName(dto.getRelativeName());
		entity.setRemark(dto.getRemark());
		entity.setStartDay(dto.getStartDay());
		entity.setTakeAway(dto.isTakeAway());
		entity.setTakeWay(dto.getTakeWay());
		entity.setDeceasedPYName(dto.getDeceasedPYName());
		entity.setRelativePYName(dto.getRelativePYName());
		entity.setAshItemPosition(dto.getAshItemPosition());
		entity.setTakeAwayDate(dto.getTakeAwayDate());
		entity.setTakeAwayPerson(dto.getTakeAwayPerson());
		entity.setTakeAwayRemark(dto.getTakeAwayRemark());
	}
}
